﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

namespace ryan
{
    class Text
    {

        private SpriteBatch sb;
        private SpriteFont font;
        private string text;
        private float rotation;
        private int borderSize;
        private Vector2 origin;
        public Vector2 Position;
        public Color ColorBack, ColorFront;
        public float Scale;

        public Text(string newText, Color newBackColor, Color newFrontColor, float newScale, int newBorderSize, float newRotation, Vector2 newPosition)
        {
            this.text = newText;
            this.ColorBack = newBackColor;
            this.ColorFront = newFrontColor;
            this.Scale = newScale;
            this.rotation = newRotation;
            this.borderSize = newBorderSize;
            this.Position = newPosition;      
        }

        public void SetBorder()
        {
            //These 4 draws are the background of the text and each of them have a certain displacement each way.
            sb.DrawString(font, text, Position + new Vector2(borderSize * Scale, borderSize * Scale), ColorBack, rotation, origin, Scale, SpriteEffects.None, 1f);
            sb.DrawString(font, text, Position + new Vector2(-borderSize * Scale, -borderSize * Scale), ColorBack, rotation, origin, Scale, SpriteEffects.None, 1f);
            sb.DrawString(font, text, Position + new Vector2(-borderSize * Scale, borderSize * Scale), ColorBack, rotation, origin, Scale, SpriteEffects.None, 1f);
            sb.DrawString(font, text, Position + new Vector2(borderSize * Scale, -borderSize * Scale), ColorBack, rotation, origin, Scale, SpriteEffects.None, 1f);
        }

        public void SetOrigin()
        {
            //If we want to draw the text from the origin we need to find that point, otherwise you can set all origins to Vector2.Zero.
            this.origin = new Vector2(this.font.MeasureString(this.text).X / 2, this.font.MeasureString(this.text).Y / 2);
        }

        public void Draw(SpriteBatch newSb, SpriteFont newFont)
        {
            this.font = newFont;
            this.sb = newSb;

            SetOrigin();

            if(this.borderSize > 0)
            {
                SetBorder();
            }

            //This is the top layer which we draw in the middle so it covers all the other texts except that displacement.
            sb.DrawString(font, text, Position, ColorFront, rotation, origin, Scale, SpriteEffects.None, 1f);
        }
    }
}
